package com.gamecollection.fish.controller;

import com.gamecollection.mvc.Controller;
import com.gamecollection.fish.model.FishModel;
import com.gamecollection.fish.utils.GameUtils;

import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;

/**
 * 大鱼吃小鱼游戏控制器
 */
public class FishController implements Controller {
    
    private FishModel model;
    
    public FishController(FishModel model) {
        this.model = model;
    }
    
    @Override
    public void initialize() {
        // 控制器初始化
    }
    
    @Override
    public void handleKeyPressed(KeyEvent e) {
        int state = model.getGameState();
        
        switch (e.getKeyCode()) {
            case KeyEvent.VK_W:
                GameUtils.UP = true;
                break;
            case KeyEvent.VK_S:
                GameUtils.DOWN = true;
                break;
            case KeyEvent.VK_A:
                GameUtils.LEFT = true;
                break;
            case KeyEvent.VK_D:
                GameUtils.RIGHT = true;
                break;
            case KeyEvent.VK_SPACE:
                if (state == FishModel.STATE_PLAYING) {
                    model.setGameState(FishModel.STATE_PAUSED);
                } else if (state == FishModel.STATE_PAUSED) {
                    model.setGameState(FishModel.STATE_PLAYING);
                }
                break;
        }
    }
    
    @Override
    public void handleKeyReleased(KeyEvent e) {
        switch (e.getKeyCode()) {
            case KeyEvent.VK_W:
                GameUtils.UP = false;
                break;
            case KeyEvent.VK_S:
                GameUtils.DOWN = false;
                break;
            case KeyEvent.VK_A:
                GameUtils.LEFT = false;
                break;
            case KeyEvent.VK_D:
                GameUtils.RIGHT = false;
                break;
        }
    }
    
    @Override
    public void handleMouseClicked(MouseEvent e) {
        int state = model.getGameState();
        
        if (e.getButton() == MouseEvent.BUTTON1) {
            if (state == FishModel.STATE_NOT_STARTED) {
                model.setGameState(FishModel.STATE_PLAYING);
            } else if (state == FishModel.STATE_GAME_OVER || state == FishModel.STATE_WIN) {
                model.reset();
                model.setGameState(FishModel.STATE_PLAYING);
            }
        }
    }
    
    @Override
    public void handleMouseMoved(MouseEvent e) {
        // 鼠标移动事件处理
    }
    
    @Override
    public void update() {
        // 控制器更新
    }
    
    @Override
    public void startGame() {
        model.setGameState(FishModel.STATE_PLAYING);
    }
    
    @Override
    public void pauseGame() {
        if (model.getGameState() == FishModel.STATE_PLAYING) {
            model.setGameState(FishModel.STATE_PAUSED);
        }
    }
    
    @Override
    public void resumeGame() {
        if (model.getGameState() == FishModel.STATE_PAUSED) {
            model.setGameState(FishModel.STATE_PLAYING);
        }
    }
    
    @Override
    public void endGame() {
        model.setGameState(FishModel.STATE_GAME_OVER);
    }
} 